package sjg.DataStructures.linkedList;


/**
 * 双向链表
 */
public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        System.out.println("测试新增数据");
        HeroNode2 hero1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 hero2 = new HeroNode2(2, "林冲", "豹子头");
        HeroNode2 hero3 = new HeroNode2(3, "卢俊义", "玉麒麟");
        HeroNode2 hero4 = new HeroNode2(4, "吴用", "智多星");
        doubleLinkedList.add(hero1);
        doubleLinkedList.add(hero2);
        doubleLinkedList.add(hero3);
        doubleLinkedList.add(hero4);
        doubleLinkedList.list();

        System.out.println("测试删除");
        HeroNode2 hero5 = new HeroNode2(3, "卢俊义", "玉麒麟");
        doubleLinkedList.delete(hero5);
        doubleLinkedList.list();

        System.out.println("测试修改");
        HeroNode2 hero6 = new HeroNode2(4, "卢俊义", "玉麒麟");
        doubleLinkedList.update(hero6);
        doubleLinkedList.list();



    }
}

// 定义 SingleLinkedList 管理我们的英雄
class DoubleLinkedList {
    public HeroNode2 head = new HeroNode2(0, "", "");

    // 新增
    public void add(HeroNode2 next) {
        HeroNode2 tempHead = head;
        // 遍历到最后一位
        while (tempHead.next != null) {
            tempHead = tempHead.next;
        }
        // 赋值
        tempHead.next = next;
        next.pre = tempHead;
    }

    // 删除
    public void delete(HeroNode2 delNode) {
        if (head.next == null) {
            System.out.println("链表为空~~~");
            return;
        }
        HeroNode2 tempHead = head;
        // 遍历判断
        while (tempHead.next != null) {
            if (tempHead.next.no == delNode.no) {
                // 匹配到，替换node, 两种情况，后续有值，后续没值
                if (tempHead.next.next == null) {
                    tempHead.next = null;
                }else {
                    tempHead.next.next.pre = tempHead.next.next;
                    tempHead.next = tempHead.next.next;
                }
                return;
            }
            tempHead = tempHead.next;
        }
        System.out.println("没有匹配值~~~");
    }

    // 修改
    public void update(HeroNode2 upNode) {
        if (head.next == null) {
            System.out.println("链表为空~~~");
            return;
        }
        HeroNode2 tempHead = head;
        // 遍历判断
        while (tempHead.next != null) {
            if (tempHead.next.no == upNode.no) {
                // 匹配到，将值修改
                tempHead.next.name = upNode.name;
                tempHead.next.nickname = upNode.nickname;
                return;
            }
            tempHead = tempHead.next;
        }
        System.out.println("没有匹配值~~~");
    }


    // 遍历双向链表
    public void list() {
        if (head.next == null) {
            System.out.println("链表为空！");
            return;
        }
        HeroNode2 tempHead = head;
        while (tempHead.next != null) {
            System.out.println(tempHead.next.toString());
            tempHead = tempHead.next;
        }
    }

}

// 定义node2
class HeroNode2 {
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 next; // 指向下一个节点，默认为null
    public HeroNode2 pre; // 指向前一个节点，默认为null

    // 构造器
    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
